﻿using BusinessLayer.VDataEntities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Security;
using TrialManagmentTool.Helper;
using TrialManagmentTool.Models;

namespace TrialManagementTool.Helper
{
    public static class UserHelper
    {
        public static VDataEntity GetCurrentAdminUser()
        {
            VDataEntity CurrentUser = new VDataEntity();
#if (DEBUG)
            {
                if (string.IsNullOrEmpty(CurrentUser.Name))
                {
                    CurrentUser = new VDataEntity { ID = 1, Name = "TestAdmin" };
                }
                else
                {
                    CurrentUser = System.Web.HttpContext.Current.Session["AdminInfo"] as VDataEntity;
                }
            }
#else
            {
                CurrentUser = System.Web.HttpContext.Current.Session["AdminInfo"] as VDataEntity;
                if (CurrentUser == null)
                {
                    if (!string.IsNullOrEmpty(UserHelper.GetUserInfoFromCookie()))
                    {
                        CurrentUser = JsonHelper.ParseFromJson<VDataEntity>(UserHelper.GetUserInfoFromCookie());
                    }
                }
            }
#endif
            //  System.Web.HttpContext.Current.Session["AdminInfo"] as VDataEntity;

            return CurrentUser;
        }

        public static VDataEntity GetCurrentPromoterUser()
        {
            VDataEntity CurrentUser = new VDataEntity();
#if (DEBUG)
            {
                if (string.IsNullOrEmpty(CurrentUser.Name))
                {
                    CurrentUser = new VDataEntity { ID = 327, Name = "测试用户" };
                }
                else
                {
                    CurrentUser = System.Web.HttpContext.Current.Session["PromoterInfo"] as VPromoter;
                }
            }
#else
            {
                CurrentUser = System.Web.HttpContext.Current.Session["PromoterInfo"] as VPromoter;
                if (CurrentUser == null)
                {
                    if (!string.IsNullOrEmpty(UserHelper.GetUserInfoFromCookie()))
                    {
                        CurrentUser = JsonHelper.ParseFromJson<VPromoter>(UserHelper.GetUserInfoFromCookie());
                    }
                }
            }
#endif

            return CurrentUser;
        }

        public static string GetWeixinUserIdFromCookie()
        {
            HttpCookie aCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName + "userid"];
            string userid = "";
            if (aCookie != null && !string.IsNullOrEmpty(aCookie.Value))
            {
                userid = aCookie.Value;
            }
            return userid;
        }

        public static void SaveUserInfoToCookie(string userInfo)
        {
            //添加userid到cookie
            var cookie = new HttpCookie(FormsAuthentication.FormsCookieName + "userinfo", HttpUtility.UrlEncode(userInfo));
            cookie.HttpOnly = true;
            //cookie.Expires = DateTime.Now.AddDays(1);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        public static string GetUserInfoFromCookie()
        {
            HttpCookie aCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName + "userinfo"];
            string userid = "";
            if (aCookie != null && !string.IsNullOrEmpty(aCookie.Value))
            {
                userid = HttpUtility.UrlDecode(aCookie.Value);
                if (string.IsNullOrEmpty(userid))
                {
                    userid = GetUserInfoFromHttpCookie();
                }
            }
            else
            {
                userid = GetUserInfoFromHttpCookie();
            }
            return userid;
        }

        public static void DeleteUserInfoCookie()
        {
            HttpCookie aCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName + "userinfo"];
            if (aCookie != null)
            {
                aCookie.Expires = DateTime.Now.AddDays(-1);
                HttpContext.Current.Response.Cookies.Add(aCookie);

            }
        }

        public static string GetUserInfoFromHttpCookie()
        {
            string cookievariable = HttpContext.Current.Request.ServerVariables.Get("Http_Cookie");
            if (!string.IsNullOrEmpty(cookievariable))
            {
                string[] cookies = cookievariable.Split(';');
                foreach (var item in cookies)
                {
                    if (item.Contains(FormsAuthentication.FormsCookieName + "userinfo"))
                    {
                        return HttpUtility.UrlDecode(item.Remove(0, (FormsAuthentication.FormsCookieName + "userinfo=").Length + 1));
                    }
                }
            }

            return "";
        }


        public static string ErrorPageDisplay()
        {
            VDataEntity CurrentUser = System.Web.HttpContext.Current.Session["AdminInfo"] as VDataEntity;

            if (CurrentUser == null)
            {
                return "未登录";
            }
            else
            {
                return CurrentUser.Name;
            }
        }
    }
}